{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#--coding:utf-8 --\n",
    "#导入 numpy\n",
    "import numpy as np\n",
    "#导入opencv\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#打开图片文件\n",
    "img = cv2.imread('lenna.jpg')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "函数简介\n",
    "1、split—提取R、B、G分量(返回值顺序为：B、G、R)\n",
    "函数原型：split(m, mv=None)\n",
    "m：彩图矩阵\n",
    "mv：默认参数\n",
    "2、merge—合并R、G、B(参数顺序为：B、G、R)\n",
    "函数原型：merge(mv, dst=None)\n",
    "m：B、G、R分量\n",
    "mv：默认参数\n",
    "3、cvtColor—合并R、G、B(参数顺序为：B、G、R)\n",
    "函数原型：cvtColor(src, code, dst=None, dstCn=None)\n",
    "src：图像矩阵\n",
    "code：转化参数\n",
    "其他：默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#显示图片\n",
    "cv2.imshow(\"Hello opencv \", img)\n",
    "cv2.waitKey(0)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#R、G、B分量的提取\n",
    "(B,G,R) = cv2.split(img)\n",
    "#提取R、G、B分量\n",
    "cv2.imshow(\"Red\",R)\n",
    "cv2.imshow(\"Green\",G)\n",
    "cv2.imshow(\"Blue\",B)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#R、G、B的合并\n",
    "merged = cv2.merge([B,G,R])#合并R、G、B分量\n",
    "cv2.imshow(\"Merged\",merged)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#彩色转灰色\n",
    "gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)\n",
    "cv2.imshow(\"Gray\",gray)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#RGB转HSV空间\n",
    "hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)\n",
    "cv2.imshow(\"HSV\",hsv)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#RGB转lab空间\n",
    "lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)\n",
    "cv2.imshow(\"L*a*b*\", lab)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "颜色空间转换\n",
    "cv2.cvtColor\n",
    "RGB就是指Red,Green和Blue,一副图像由这三个channel(通道)构成\n",
    "Gray就是只有灰度值一个channel\n",
    "HSV即Hue(色调),Saturation(饱和度)和Value(亮度)三个channel\n",
    "RGB是为了让机器更好的显示图像,对于人类来说并不直观,HSV更为贴近我们的认知,所以通常我们在针对某种颜色做提取时会转换到HSV颜色空间里面来处理. "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
